package org.orbis.search;

import java.io.PrintWriter;
import org.orbis.structure.ContentInfo;
import org.orbis.tool.Header;

public class SearchForm{
	private ContentInfo contentInfo;
	private PrintWriter out;

	public SearchForm(ContentInfo contentInfo, PrintWriter writer){
		this.contentInfo = contentInfo;
		this.out = writer;
	}

	public void putForm(){
		// Dialogue searching
		out.println("<!DOCTYPE html>");
		out.println("<html>");
		out.println("<head>");
		out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
		out.println("<title>ORBIS Search System: Search</title>");
		out.println("</head>");
		out.println("<body>");
		// Title bar
		out.println(Header.getTitleBar("ORBIS Search System: Search"));

		// Header
		out.println(Header.getHeader(2));
		
		out.println("Please input value:");
		out.println("<form method=\"POST\" action=\"search\">");

		out.println("<table>");
		out.println("<tbody>");
		out.println("<tr>");
		out.println("<td>");
		out.println("Query");
		out.println("</td>");
		out.println("<td>");
		out.println("<textarea col=5 row=100 name=\"query\" required>");
		out.println("</textarea>");
		out.println("</td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("<td>");
		out.println("Result from");
		out.println("</td>");
		out.println("<td>");
		out.println("<input type=\"number\" name=\"from\" required>");
		out.println("</td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("<td>");
		out.println("Result size");
		out.println("</td>");
		out.println("<td>");
		out.println("<input type=\"number\" name=\"length\" required>");
		out.println("</td>");
		out.println("</tr>");

		out.println("<tr>");
		out.println("<td>");
		out.println("Operator");
		out.println("</td>");
		out.println("<td>");
		out.println("<input type=\"radio\" name=\"operator\" value=\"or\" checked=\"checked\"> OR  ");
		out.println("<input type=\"radio\" name=\"operator\" value=\"and\"> AND");
		out.println("</td>");
		out.println("</tr>");

		out.println("<tr>");
		out.println("<td>");
		out.println("Target field:");
		out.println("</td>");
		out.println("<td></td>");
		out.println("</tr>");
		for(int i = 0; i < contentInfo.contentsNum(); i++){
			out.println("<tr>");
			out.println("<td align=\"right\">");
			out.println("<input type=\"checkbox\" name=\"target\" value=\"" + contentInfo.contentName(i)
				+ "\" checked=\"checked\">");
			out.println("</td>");
			out.println("<td>");
			out.println(contentInfo.contentName(i));
			out.println("</td>");
			out.println("</tr>");
		}

		for(int i = 0; i < contentInfo.binaryFlagNum(); i++){
			out.println("<tr>");
			out.println("<td>");
			out.println(contentInfo.binaryFlagName(i));
			out.println("</td>");
			out.println("<td>");
			out.println("<input type=\"radio\" name=\"" + contentInfo.binaryFlagName(i)
				+ "\" value=\"true\"> True   ");
			out.println("<input type=\"radio\" name=\"" + contentInfo.binaryFlagName(i)
				+ "\" value=\"false\"> False ");
			out.println("</td>");
			out.println("</tr>");
		}

		for(int i = 0; i < contentInfo.integerFlagNum(); i++){
			out.println("<tr>");
			out.println("<td>");
			out.println(contentInfo.integerFlagName(i));
			out.println("</td>");
			out.println("<td>");
			out.println("<select name=\"" + contentInfo.integerFlagName(i) + "\">");
			out.println("<option value=\"off\">? (Don't care)</option>");
			out.println("<option value=\"equal\">x = a (Equal)</option>");
			out.println("<option value=\"notequal\">x != a (Not equal)</option>");
			out.println("<option value=\"greater\">x &gt;= a (Greater)</option>");
			out.println("<option value=\"less\">x &lt;= a (Less or equal)</option>");
			out.println("<option value=\"in\">a &lt;= x &lt;= b (In the range)</option>");
			out.println("<option value=\"out\">x &lt;= a, b &lt;= x (Out of range)</option>");
			out.println("</select>");
			out.println("a = <input type=\"number\" name=\"" + contentInfo.integerFlagName(i)
				+ "\" value=\"0\">, ");
			out.println(" b = <input type=\"number\" name=\"" + contentInfo.integerFlagName(i)
				+ "\" value=\"0\">");
			out.println("</td>");
			out.println("</tr>");
		}

		for(int i = 0; i < contentInfo.longFlagNum(); i++){
			out.println("<tr>");
			out.println("<td>");
			out.println(contentInfo.longFlagName(i));
			out.println("</td>");
			out.println("<td>");
			out.println("<select name=\"" + contentInfo.longFlagName(i) + "\">");
			out.println("<option value=\"off\">? (Don't care)</option>");
			out.println("<option value=\"equal\">x = a (Equal)</option>");
			out.println("<option value=\"notequal\">x != a (Not equal)</option>");
			out.println("<option value=\"greater\">x &gt;= a (Greater)</option>");
			out.println("<option value=\"less\">x &lt;= a (Less or equal)</option>");
			out.println("<option value=\"in\">a &lt;= x &lt;= b (In the range)</option>");
			out.println("<option value=\"out\">x &lt;= a, b &lt;= x (Out of range)</option>");
			out.println("</select>");
			out.println("a = <input type=\"number\" name=\"" + contentInfo.longFlagName(i)
				+ "\" value=\"0\">, ");
			out.println(" b = <input type=\"number\" name=\"" + contentInfo.longFlagName(i)
				+ "\" value=\"0\">");
			out.println("</td>");
			out.println("</tr>");
		}

		for(int i = 0; i < contentInfo.floatFlagNum(); i++){
			out.println("<tr>");
			out.println("<td>");
			out.println(contentInfo.floatFlagName(i));
			out.println("</td>");
			out.println("<td>");
			out.println("<select name=\"" + contentInfo.floatFlagName(i) + "\">");
			out.println("<option value=\"off\">? (Don't care)</option>");
			out.println("<option value=\"equal\">x = a (Equal)</option>");
			out.println("<option value=\"notequal\">x != a (Not equal)</option>");
			out.println("<option value=\"greater\">x &gt;= a (Greater)</option>");
			out.println("<option value=\"less\">x &lt;= a (Less or equal)</option>");
			out.println("<option value=\"in\">a &lt;= x &lt;= b (In the range)</option>");
			out.println("<option value=\"out\">x &lt;= a, b &lt;= x (Out of range)</option>");
			out.println("</select>");
			out.println("a = <input type=\"number\" name=\"" + contentInfo.floatFlagName(i)
				+ "\" value=\"0\">, ");
			out.println(" b = <input type=\"number\" name=\"" + contentInfo.floatFlagName(i)
				+ "\" value=\"0\">");
			out.println("</td>");
			out.println("</tr>");
		}

		out.println("</tbody>");
		out.println("</table>");

		out.println("<input type=\"submit\" value=\"Search\">");
		out.println("<input type=\"reset\" value=\"Reset\">");

		out.println("</form>");
		out.println("</body>");
		out.println("</html>");
	}
}
